﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
using System.Runtime.InteropServices;

namespace nudota
{
    public partial class Chating : UserControl
    {
        [DllImport("user32.dll")]
        public static extern bool FlashWindow(
              IntPtr hWnd,           //   handle   to   window   
              bool bInvert       //   flash   status   
              );   

        public Chating()
        {
            InitializeComponent();
        }

        Image[] faces;
        private void button1_Click(object sender, EventArgs e)
        {
            //FlashWindow(FindForm().Handle, true);
            rtShowBox.AppendText("\n");
            //rtShowBox.SelectedRtf = rtInputBox.Rtf;
            rtShowBox.Text = rtInputBox.Rtf;
            rtInputBox.Clear();

            //DataObject da = new DataObject(img);
            //string[] t = da.GetFormats();
            //bool tt = da.GetDataPresent(DataFormats.Rtf);
            //rtInputBox.DoDragDrop(da, DragDropEffects.All);
            //rtShowBox.DoDragDrop(da, DragDropEffects.All);
        }

        private int m_last_distance;
        private Form fs = new Form();
        private Form fs2 = new Form();
        private void init_panel(Form fs, Color bc)
        {
            fs.ShowIcon = false;
            fs.ShowInTaskbar = false;
            fs.StartPosition = FormStartPosition.Manual;
            fs.FormBorderStyle = FormBorderStyle.None;
            fs.BackColor = bc;

            int i, j, k;
            const int line_count = 14;
            int w = faces[0].Width, w2 = w / 2;
            int h = faces[0].Height, h2 = h / 2;
            EventHandler click = new EventHandler(face_click);
            for (i = 0, k = 0; ; ++i)
                for (j = 0; j < line_count; ++j)
                {
                    PictureBox p = new PictureBox();
                    p.Image = faces[k];
                    p.Tag = k;
                    k++;
                    p.BackColor = Color.Transparent;
                    p.Top = i * (w + 2) + w2;
                    p.Left = j * (h + 2) + h2;
                    p.Width = w;
                    p.Height = h;
                    p.Cursor = Cursors.Hand;
                    p.Click += click;
                    fs.Controls.Add(p);

                    if (k == faces.Length) goto Break;
                }

        Break:
            fs.Width = (line_count + 1) * (w + 2);
            fs.Height = (i + 2) * (h + 2);
            fs.ResumeLayout();
        }

        private void Chating_Load(object sender, EventArgs e)
        {
            rtInputBox.DragOver += new DragEventHandler(rtInputBox_DragOver);
            rtInputBox.DragDrop += new DragEventHandler(rtInputBox_DragDrop);

            int i;
            string[] files = Directory.GetFiles(".\\face", "*.gif");
            Array.Sort(files);
            faces=new Image[files.Length];
            for (i = 0; i < files.Length; ++i)
                faces[i] = Image.FromFile(files[i]);

            init_panel(fs, Color.Silver);
            init_panel(fs2, Color.White);
            fs.Opacity = 0.8;
            fs.LostFocus += new EventHandler(panel1_LostFocus);
            fs2.Left = -1000;
            fs2.Visible = true;
            FindForm().AddOwnedForm(fs);
            FindForm().AddOwnedForm(fs2);
        }

        private void panel1_LostFocus(object sender, EventArgs e)
        {
            fs.Visible = false;
        }

        private void rtInputBox_DragOver(object sender, DragEventArgs e)
        {
            e.Effect = (e.Data.GetDataPresent(DataFormats.Rtf)) ?
                DragDropEffects.Copy : DragDropEffects.None;

        }

        private void rtInputBox_DragDrop(object sender, DragEventArgs e)
        {
            
        }

        private void button2_Click(object sender, EventArgs e)
        {
            //Point p = PointToClient(RichTextBox.MousePosition);
            Point p = PointToScreen(button2.Location);
            //p.X = RichTextBox.MousePosition.X;
            p.Y -= fs.Height+3;
            fs.Location = p;
            fs.Visible = true;
        }

        private void face_click(object sender, EventArgs e)
        {
            fs.Visible = false;
            Control p = fs2.Controls[(int)(sender as PictureBox).Tag];
            fs2.Controls.Add(p);
            rtInputBox.InsertControl(p);
            //p.Parent = rtInputBox;

            //Graphics g = button2.CreateGraphics();
            //g.ResetClip();
            //g.DrawImage(face, new Point(0, 0));
        }

        private void button3_Click(object sender, EventArgs e)
        {
            fontDialog1.ShowDialog();
        }

        private void button4_Click(object sender, EventArgs e)
        {
            colorDialog1.ShowDialog();
        }

        private void panel11_Click(object sender, EventArgs e)
        {
            //fs.Hide();
        }

        private void checkBox2_CheckedChanged(object sender, EventArgs e)
        {
            if (splitContainer1.Width - splitContainer1.SplitterDistance > 10)
            {
                m_last_distance = splitContainer1.SplitterDistance;
                splitContainer1.SplitterDistance = splitContainer1.Width - 5;
            }
            else splitContainer1.SplitterDistance = m_last_distance;
        }

    }
}
